package com.sky.web.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分类分页
    @GetMapping("/page")
    public Result findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageBean pageBean = categoryService.findByPage(categoryPageQueryDTO);
        return Result.success(pageBean);
    }

    //新增分类
    @PostMapping
    public Result save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    //删除分类
    @DeleteMapping
    public Result deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    //分类回显
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        Category category = categoryService.findById(id);
        return Result.success(category);
    }

    //分类修改
    @PutMapping
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    //根据类型查询
    @GetMapping("/list")
    public Result findByType(Integer id) {
        List<Category> list = categoryService.findByList(id);
        return Result.success(list);
    }

    //启动禁用
    @PostMapping("/status/{status}")
    public Result updateStatus(Long id,@PathVariable("status") Integer status){
        //1.构建Employee对象
        Category category = Category.builder().id(id).status(status).build();
        //2.调用service更新
        categoryService.updateById(category);
        //3.返回结果
        return Result.success();
    }

}
